package com.itbour.service.material.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.material.ItbComDao;
import com.itbour.dto.com.CoopAppInfoDto;
import com.itbour.dto.com.FeedbackFromInfoDto;
import com.itbour.dto.com.FeedbackInfoDto;
import com.itbour.dto.com.ItbAreaInfoDto;
import com.itbour.dto.com.PartnerAreaInfoDto;
import com.itbour.dto.com.PartnerCustInfoDto;
import com.itbour.dto.com.PartnerIncomeInfoDto;
import com.itbour.dto.com.PartnerInfoDto;
import com.itbour.dto.com.TopicInfoDto;
import com.itbour.dto.com.TopicRInfoDto;
import com.itbour.dto.tg.DesignInfoDto;
import com.itbour.dto.tg.TypeInfoDto;
import com.itbour.service.material.ItbComService;


/**
*
* copyright   itbour team 2015
* @author     xc
*
*/

public class ItbComServiceImpl implements ItbComService {

	private ItbComDao itbComDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page topicPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectTopicCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<TopicInfoDto> data = itbComDao.selectTopicInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	//获取专题下的-设计模板
	public Page getSpecialDesignInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectSpecialDesignCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<DesignInfoDto> data = itbComDao.getSpecialDesignInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	
	//获取专题下的PPT-设计模板
	public Page getSpecialPPtInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectSpecialPPtCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<DesignInfoDto> data = itbComDao.getSpecialPPtInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	
	//获取适合某个专题能添加的设计模板信息
	public Page getSuitDesignInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectSuitDesignCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<DesignInfoDto> data = itbComDao.getSuitDesign(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	
	//获取专题下合适的PPT列表信息
	public Page getSuitPPtInfo(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectSuitPPTCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<DesignInfoDto> data = itbComDao.getSuitPPTInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	public List<TopicRInfoDto> getOneTopicRInfo(Map<String, Object> parm){
		return itbComDao.getOneTopicRInfo(parm);
	}
	public List<TypeInfoDto> selectOneType(Map<String, Object> parm){
		return itbComDao.selectOneType(parm);
	}
	
	//添加适合的设计模板到专题下
	public boolean addSuitDesign(Map<String, Object> parm){
		try{
			itbComDao.addSuitDesign(parm);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	//删除专题设计模板
	public boolean delTopicDesign(Map<String, Object> parm){
		try{
			itbComDao.delTopicDesign(parm);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	//修改专题设计下的order
	public boolean udpTorder(Map<String, Object> parm){
		try{
			itbComDao.updTorder(parm);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	
	//
	public List<TopicInfoDto> selectTopicInfo(Map<String,Object> param) {
		return itbComDao.selectTopicInfo(param);
	}
	//
	public void delTopic(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		itbComDao.deleteTopicInfoLogic(param);
		itbComDao.deleteTopicRInfoLogic(param);// 一并删除关联信息
	}
	//
	public void addTopic(TopicInfoDto topicInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", topicInfo.getType());
		param.put("title", topicInfo.getTitle());
		param.put("desc", topicInfo.getDesc());
		param.put("thumb", topicInfo.getThumb());
		param.put("use", topicInfo.getUse());
		param.put("order", topicInfo.getOrder());
		param.put("update_by", topicInfo.getUpdate_by());
		itbComDao.insertTopicInfo(param);
	}
	//
	public void updTopic(Map<String,Object> param) {
		itbComDao.updateTopicInfoById(param);
	}
	
	
	// 用户反馈
	public Page feedbackPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize){
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectFeedbackCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<FeedbackInfoDto> data = itbComDao.selectFeedbackInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	//
	public List<FeedbackInfoDto> selectFeedbackInfo(Map<String,Object> param){
		return itbComDao.selectFeedbackInfo(param);
	}
	// 删除用户反馈
	public void delFeedback(String delIds){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		itbComDao.deleteFeedbackInfoLogic(param);
	}
	// 更新用户反馈
	public void updFeedback(Map<String,Object> param){
		itbComDao.updateFeedbackInfoById(param);
	}
	// 获取反馈来源
	public List<FeedbackFromInfoDto> getFeedbackFrom4Combo(Map<String,Object> param) {
		return itbComDao.selectFeedbackFromInfo(param);
	}
	
	
	// 合作申请
	public Page coopAppPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize){
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectCoopAppCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<CoopAppInfoDto> data = itbComDao.selectCoopAppInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	//
	public List<CoopAppInfoDto> selectCoopAppInfo(Map<String,Object> param){
		return itbComDao.selectCoopAppInfo(param);
	}
	// 删除合作申请
	public void delCoopApp(String delIds){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		itbComDao.deleteCoopAppInfoLogic(param);
	}
	// 更新合作申请
	public void updCoopApp(Map<String,Object> param){
		itbComDao.updateCoopAppInfoById(param);
	}
	
	
	// 合伙人
	public Page partnerPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize){
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectPartnerCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<PartnerInfoDto> data = itbComDao.selectPartnerInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	//
	public List<PartnerInfoDto> selectPartnerInfo(Map<String,Object> param){
		return itbComDao.selectPartnerInfo(param);
	}
	// 删除合伙人
	public void delPartner(String delIds){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		itbComDao.deletePartnerInfoLogic(param);
	}
	// 更新合伙人信息
	public void updPartner(Map<String,Object> param){
		itbComDao.updatePartnerInfoById(param);
	}
	// 添加合伙人信息
	public void addPartner(Map<String,Object> param) {
		itbComDao.insertPartnerInfo(param);
	}
	
	
	// 合伙人管辖区域
	public Page partnerAreaPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize){
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectPartnerCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<PartnerAreaInfoDto> data = itbComDao.selectPartnerAreaInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	//
	public List<PartnerAreaInfoDto> selectPartnerAreaInfo(Map<String,Object> param){
		return itbComDao.selectPartnerAreaInfo(param);
	}
	//
	public List<PartnerAreaInfoDto> selectPartnerAreaInfoForChk(Map<String,Object> param){
		return itbComDao.selectPartnerAreaInfoForChk(param);
	}
	// 删除合伙人管辖区域
	public void delPartnerArea(String delIds){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		itbComDao.deletePartnerAreaInfoLogic(param);
	}
	// 添加合伙人信息
	public void addPartnerArea(Map<String,Object> param) {
		itbComDao.insertPartnerAreaInfo(param);
	}
	
	// 获取区域列表
	public List<ItbAreaInfoDto> getAreaForCombo(Map<String,Object> param) {
		return itbComDao.selectItbAreaInfo(param);
	}
		
	
	// 合伙人收益
	public Page partnerIncomePgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize){
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectPartnerIncomeCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<PartnerIncomeInfoDto> data = itbComDao.selectPartnerIncomeInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	//
	public List<PartnerIncomeInfoDto> selectPartnerIncomeInfo(Map<String,Object> param){
		return itbComDao.selectPartnerIncomeInfo(param);
	}
	
	// 删除合伙人收益
	public void delPartnerIncome(String delIds){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		itbComDao.deletePartnerIncomeInfoLogic(param);
	}
	// 更新合伙人收益
	public void updPartnerIncome(Map<String,Object> param){
		itbComDao.updatePartnerIncomeInfoById(param);
	}
	// 添加合伙人收益信息
	public void addPartnerIncome(Map<String,Object> param) {
		itbComDao.insertPartnerIncomeInfo(param);
	}
	// 计算合伙人收益信息
	public Long calPartnerIncome(Map<String,Object> param) {
		return itbComDao.calPartnerIncome(param);
	}
	public Long calPartnerIncomeEx(Map<String,Object> param) {
		return itbComDao.calPartnerIncomeEx(param);
	}
	public Long calPartnerBonus(Map<String,Object> param) {
		return itbComDao.calPartnerBonus(param);
	}
	
	
	// 合伙人下线用户
	public Page partnerCustPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize){
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itbComDao.selectPartnerCustCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<PartnerCustInfoDto> data = itbComDao.selectPartnerCustInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	// 更新合伙人下线的有效截止日
	public void updPartnerCustEndTime(Map<String,Object> param) {
		itbComDao.updatePartnerCustEndTimeByUser(param);
	}
	
		
	public ItbComDao getItbComDao() {
		return itbComDao;
	}

	public void setItbComDao(ItbComDao itbComDao) {
		this.itbComDao = itbComDao;
	}
}
